package com.isaiah.queue;

import com.isaiah.list.ArrayList;

/**
 * @Title: 基于数组的队列实现
 * @Description: ${todo}
 * @author: Haijun
 * @emial: isaiah@sohu.com
 * @date 2020/4/815:59
 */
public class ArrayListQueue<E> implements Queue<E> {
    private ArrayList<E> array;

    public ArrayListQueue(int capacity) {
        this.array = new ArrayList<>(capacity);
    }

    public ArrayListQueue() {
        this.array = new ArrayList<>();
    }

    @Override
    public int getSize() {
        return this.array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.array.isEmpty();
    }

    @Override
    public void enqueue(E e) {
        this.array.addLast(e);
    }

    @Override
    public E dequque() {
        return this.array.removeFirst();
    }

    @Override
    public E getFront() {
        return this.array.getFirst();
    }

    @Override
    public String toString() {
        StringBuffer rs = new StringBuffer();
        rs.append(String.format("Queue: size = %d, capacity = %d\n", this.array.getSize(), this.array.getCapacity()));
        rs.append("front [");
        for (int i = 0; i < this.array.getSize(); i ++){
            rs.append(this.array.get(i));
            if (i != this.array.getSize() -1){
                rs.append(", ");
            }
        }
        rs.append("] tail");
        return rs.toString();
    }
}
